<?php

namespace app\command;

use app\model\NewsIssuer;
use app\tool\ZhConvert;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use isszz\sensitive\facade\Sensitive;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;

class Caiji extends Command
{
	protected function configure()
	{
		$this->setName('Caiji')
			->addOption('type', null, Option::VALUE_REQUIRED, '类目')
			->setDescription('采集新闻');
	}
	
	protected function execute(Input $input, Output $output)
	{
		try {
			if ($input->hasOption('type')) {
				switch ($input->getOption('type')){
					case '香港':
						$url='https://news.google.com/topics/CAAqJQgKIh9DQkFTRVFvSUwyMHZNRE5vTmpRU0JYcG9MVWhMS0FBUAE?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=2;
						break;
					case '國際':
						$url='https://news.google.com/topics/CAAqKggKIiRDQkFTRlFvSUwyMHZNRGx1YlY4U0JYcG9MVWhMR2dKSVN5Z0FQAQ?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=3;
						
						break;
					case '商業':
						$url='https://news.google.com/topics/CAAqKggKIiRDQkFTRlFvSUwyMHZNRGx6TVdZU0JYcG9MVWhMR2dKSVN5Z0FQAQ?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=1;
						
						break;
					case '娛樂':
						$url='https://news.google.com/topics/CAAqKggKIiRDQkFTRlFvSUwyMHZNREpxYW5RU0JYcG9MVWhMR2dKSVN5Z0FQAQ?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=4;
						
						break;
					case '體育':
						$url='https://news.google.com/topics/CAAqKggKIiRDQkFTRlFvSUwyMHZNRFp1ZEdvU0JYcG9MVWhMR2dKSVN5Z0FQAQ?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=5;
						break;
					case '科技':
						$url='https://news.google.com/topics/CAAqLAgKIiZDQkFTRmdvSkwyMHZNR1ptZHpWbUVnVjZhQzFJU3hvQ1NFc29BQVAB?hl=zh-HK&gl=HK&ceid=HK%3Azh-Hant';
						$class_id=6;
						break;
					default:
						$this->output->write('Category input error',true);
						exit;
					
				}
			} else {
				$this->output->write('Please enter the collection category',true);
				exit;
			}
			
			
			$issuer=NewsIssuer::where(['open'=>1])->select();
			$contents= $this->curlRequest($url);
			
			preg_match_all('/<article class="IBr9hb"/',$contents,$ma,PREG_OFFSET_CAPTURE);
			
			foreach ($ma[0] as $v){
				$start=$v[1];
				$end=stripos($contents,'</article>',$start+1);
				$str=substr($contents,$start,$end-$start);
				
				$aaa=strripos($str,'class="vr1PYe" data-n-tid="9">');
				$bbb=substr($str,$aaa,60);
				$news_tv=str_ireplace('class="vr1PYe" data-n-tid="9">', '', $bbb);
				$db_news_tv=substr_replace($news_tv,'',stripos($news_tv,'</div>'));
				$this->output->write($db_news_tv,true);
				
				$search=false;
				foreach ($issuer as $issuer_v){
					if ($issuer_v['name']==$db_news_tv){
						$search=$issuer_v;
					}
				}
				
				if (!$search){
					continue;
				}
				//筛除视频
				if (strripos($str,'class="WSA48b NMm5M"')!==false){
					continue;
				}
				$ccc=strripos($str,'jsaction="click:kkIcoc;">');
				$eee=stripos($str,'</a>',$ccc+1);
				$db_title=str_replace('jsaction="click:kkIcoc;">','',substr($str,$ccc,$eee-$ccc));
				$jjj=stripos($str,'href=".');
				$kkk=stripos($str,'" target="_blank"',$jjj+1);
				$url='https://news.google.com'.str_replace('href=".','',substr($str,$jjj,$kkk-$jjj));
				$content=$this->curlRequest($url);
				$lll=stripos($content,'<a href="');
				$mmm=stripos($content,'" jsname="tljFtd"',$lll+1);
				$new_url=str_replace('<a href="','',substr($content,$lll,$mmm-$lll));
				
				
				if (in_array($db_news_tv,['on.cc東網','信報財經新聞','Initium Media 端傳媒','經濟一週','AM730'])){
					$content=$this->curlRequest($new_url,['User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0']);
				}else{
					$content=$this->curlRequest($new_url);
				}
				$this->output->write($new_url,true);
				switch ($db_news_tv){
					case '香港01':
						try {
							preg_match_all('/<p class="break-words">/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
							preg_match_all('/"mainImage":/',$content,$sa,PREG_OFFSET_CAPTURE);//图片
							$len=end($ma[0])[1];
							$start=stripos($content,"<article id=\"article-content-section\"");
							$end=stripos($content,'</p>',$len+1);
							$ooo=substr($content,$start,$end-$start);
							$db_content= trim(strip_tags(str_replace('c<article id="article-content-section','',$ooo)));
							$image_url=[];
							foreach ($sa[0] as $vss){
								$start=$vss[1];
								$end=stripos($content,'}',$start+1);
								$ooo=substr($content,$start,$end-$start+1);
								$images_arr=json_decode(str_replace('"mainImage":','',$ooo),true);
								$image_url[]=$images_arr['cdnUrl'];
							}
							$db_images=implode(',',$image_url);
						}catch (Exception $e){
							$this->output->write($e->getMessage(),true);
							continue 2;
						}
						break;
					case '香港電台':
						preg_match_all('/class="itemFullText">/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</div>',$start+1);
							$nnn.=substr($content,$start,$end-$start);
						}
						$db_content=trim(strip_tags(str_replace('class="itemFullText">','',$nnn)));
						
						$start=stripos($content,"<img class='imgPhotoAfterLoad'  src=\"");
						if ($start!==false){
							$end=stripos($content,'"',$start+40);
							$ooo=substr($content,$start,$end-$start);
							$ooo=str_replace("<img class='imgPhotoAfterLoad'  src=\"",'',$ooo);
							$db_images=$ooo;
						}else{
							$db_images='';
						}
						break;
					case '雅虎香港新聞':
						$start= stripos($content,"<div class=caas-body>");
						$end=stripos($content,'</article>',$start+1);
						$content=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$nnn.=substr($content,$start,$end-$start);
							
						}
						$db_content=$nnn.'</p>';
						
						$start= stripos($content,"<figure class=caas-figure>");
						$end=stripos($content,'</figure>',$start+1);
						$content=substr($content,$start,$end-$start);
						preg_match_all('/data-src=/',$content,$sa,PREG_OFFSET_CAPTURE);//图片
						$image_url=[];
						foreach ($sa[0] as $vss){
							$start=$vss[1];
							$end=stripos($content,'>',$start+1);
							$images=substr($content,$start,$end-$start);
							$image_url[]=str_replace('data-src=','',$images);
						}
						$db_images=implode(',',$image_url);
						break;
					case '星島頭條':
						$start= stripos($content,"<div id=\"conimg\"");
						$end=stripos($content,'<div class="share-box"',$start+1);
						$ooo=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						preg_match_all('/field--item">  <img src="/',$content,$sa,PREG_OFFSET_CAPTURE);//图片
						
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start);
						}
						$db_content=trim(strip_tags($nnn));
						$image_url=[];
						foreach ($sa[0] as $vss){
							$start=$vss[1];
							$end=stripos($content,'"',$start+30);
							$ooo=substr($content,$start,$end-$start);
							$img=str_replace('field--item">  <img src="','',$ooo);
							$image_url[]=$img;
							
						}
						$db_images=implode(',',$image_url);
						break;
					case '明報新聞網':
						$start= stripos($content,"<article class=\"txt4\">");
						$end=stripos($content,'</article>',$start+1);
						$ooo=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start);
						}
						$db_images='';
						$db_content=trim(strip_tags($nnn));
						break;
					case 'on.cc東網':
						$start= stripos($content,"ONCC.Content.data = [");
						$end=stripos($content,'];',$start+1);
						$content=substr($content,$start,$end-$start);
						$json_info=str_replace('ONCC.Content.data = [','',$content);
						$json_info = json_decode($json_info);
						$db_content = str_replace('&lt;br/&gt;','',$json_info->content);
						$image_url=[];
						if (!empty($json_info->photo)){
							$client = new Client();
							try {
								$client->get('https://hk.on.cc/hk/bkn'.$json_info->photo[0]->thumbnail_1);
								$domain = 'https://hk.on.cc/hk/bkn';
							} catch (GuzzleException $e) {
								$domain = 'https://hk.on.cc';
							}
							foreach ($json_info->photo as $kss=>$vss){
								$thumbnail = 'thumbnail_'.($kss+1);
								$images = $domain.$vss->$thumbnail;
								$image_url[] = $images;
							}
							$db_images=implode(',',$image_url);
						}else{
							$db_images='';
						}
						break;
					case 'now 新闻':
						preg_match_all('/<div class="contentTxt"><p>/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p></div>',$start+1);
							$text=substr($content,$start,$end-$start);
							$ccc=strripos($text,'<a href="');
							if ($ccc){
								continue;
							}
							$nnn.=str_replace('<div class="contentTxt"><p>','',$text);
						}
						$db_content=trim(strip_tags($nnn));
						break;
					case '巴士的報':
						$start=strpos($content,'<div class="article-body">');
						$end=strpos($content,'<iframe id="bp-vote-iframe" scrolling="no" frameborder="0"');
						$content=substr($content,$start,$end-$start);
						
						preg_match_all('/<p class="font-size-scaler-control" data-font="16">/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
						preg_match_all('/<a href="/',$content,$images,PREG_OFFSET_CAPTURE);//图片
						
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$text=substr($content,$start,$end-$start);
							$nnn.=str_replace('<p class="font-size-scaler-control" data-font="16">','',$text);
						}
						$image_url=[];
						foreach ($images[0] as $vss){
							$start=$vss[1];
							$end=stripos($content,'"',$start+16);
							$image=substr($content,$start,$end-$start);
//			            $image_url[]=$this->http_down(str_replace('<a href="','',$image));
							$image_url[]=str_replace('<a href="','',$image);
						}
						$db_images=implode(',',$image_url);
						$db_content=trim(strip_tags($nnn));
						break;
					case '香港文匯網':
						$start=strpos($content,'<div class="post-body">');
						$end=strpos($content,'<div class="post-footer">');
						$content=substr($content,$start,$end-$start);
						
						preg_match_all('/<p>/',$content,$texts,PREG_OFFSET_CAPTURE);//文字
						preg_match_all('/<img src="/',$content,$images,PREG_OFFSET_CAPTURE);//图片
						
						$nnn='';
						foreach ($texts[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$text=substr($content,$start,$end-$start);
							$nnn.=str_replace('<p>','',$text);
						}
						
						foreach ($images[0] as $vss){
							$start=$vss[1];
							$end=stripos($content,'"',$start+15);
							$image=substr($content,$start,$end-$start);
							$image_url[]=str_replace('<img src="','',$image);
						}
						$db_images=implode(',',$image_url);
						$db_content=trim(strip_tags($nnn));
						break;
					case '晴報 Sky Post':
						$start=strpos($content,'<div class="content-section-region__article-details">');
						$end=strpos($content,'<div class="col-xs-12 article-details-img-inline-container">');
						$content=substr($content,$start,$end-$start);
						
						preg_match_all('/<p>/',$content,$texts,PREG_OFFSET_CAPTURE);//文字
						
						$nnn='';
						foreach ($texts[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$text=substr($content,$start,$end-$start);
							$nnn.=str_replace('<p>','',$text);
						}
						$db_images='';
						$db_content=trim(strip_tags($nnn));
						break;
					case '香港經濟日報 - 即時新聞':
						$start=strpos($content,'<div class="article-detail-content-container">');
						$end=strpos($content,'<div class="contentTags-container">');
						$content=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$content,$texts,PREG_OFFSET_CAPTURE);//文字
						preg_match_all('/data-src="/',$content,$images,PREG_OFFSET_CAPTURE);//图片
						
						$nnn='';
						foreach ($texts[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$text=substr($content,$start,$end-$start);
							$nnn.=str_replace('&nbsp;','',trim(strip_tags(str_replace('<p>','',$text))));
						}
						foreach ($images[0] as $vss){
							$start=$vss[1];
							$end=stripos($content,'"',$start+15);
							$image=substr($content,$start,$end-$start);
							$image_url[]=str_replace('data-src="','',$image);
						}
						$db_content=trim(strip_tags($nnn));
						$db_images=implode(',',$image_url);
						break;
					case '大紀元時報-香港':
						preg_match_all('/<p>/',$content,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($content,'</p>',$start+1);
							$nnn.=substr($content,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$start=stripos($content,"<a class=\"image-link\" href=\"");
						$end=stripos($content,'"',$start+40);
						$ooo=substr($content,$start,$end-$start);
						$ooo=str_replace("<a class=\"image-link\" href=\"",'',$ooo);
						$db_images=$ooo;
						break;
					case '獨立媒體 inmediahk.net':
						$start=stripos($content,"<div class=\"field-item even\">");
						$end=stripos($content,'</div>',$start+10);
						$ooo=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$db_images='';
						break;
					case '頭條日報':
						$start=stripos($content,'<section class="d-md-flex py-3">');
						$end=stripos($content,'</section>',$start+10);
						$ooo=substr($content,$start,$end-$start+10);
						
						$db_content=$ooo;
						$db_images='';
						break;
					case '信報財經新聞':
						$start=stripos($content,'<div id="article-detail-wrapper">');
						$end=stripos($content,'<div id="related-articles-wrapper">',$start+40);
						$ooo=substr($content,$start,$end-$start+10);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$db_images='';
						break;
					case 'Yahoo':
						$start=stripos($content,'<div class=caas-body>');
						$end=stripos($content,'</div>',$start+10);
						$ooo=substr($content,$start,$end-$start+10);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$start=stripos($content,'<div class=caas-img-container');
						if ($start!==false){
							$end=stripos($content,'</div>',$start+10);
							$ooo=substr($content,$start,$end-$start+10);
							$start=stripos($ooo,'data-src=');
							$end=stripos($ooo,'>',$start+10);
							$ooo=substr($ooo,$start,$end-$start);
							$ooo=str_replace('data-src=','',$ooo);
							$db_images=$ooo;
						}else{
							$db_images='';
						}
					
						break;
					case 'Initium Media 端傳媒':
						$start=stripos($content,'<script id="__NEXT_DATA__" type="application/json">');
						$end=stripos($content,'</script>',$start+10);
						$ooo=substr($content,$start,$end-$start);
						$ooo=str_replace('<script id="__NEXT_DATA__" type="application/json">','',$ooo);
						$ooo=json_decode($ooo);
						$db_content=$ooo->props->pageProps->dehydratedState->queries[0]->state->data->content;
						$db_images='';
						break;
					case 'RFI - 法國國際廣播電台':
						$start=stripos($content,'<div class="t-content__body u-clearfix">');
						$end=stripos($content,'<div class="o-self-promo o-self-promo--app o-self-promo--hidden" data-selfpromo-app>',$start+10);
						$ooo=substr($content,$start,$end-$start+10);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$db_images='';
						break;
					case '經濟一週':
						$start=stripos($content,'<div class="_content_ AdAsia" is_normal="yes">');
						$end=stripos($content,'<p><input type="hidden"',$start+10);
						$ooo=substr($content,$start,$end-$start);
						$db_content=$ooo;
						$db_images='';
						break;
					case '港生活':
						$start=stripos($content,'<div id="content-main"');
						$end=stripos($content,'<section class="ad-zone-textlink"',$start);
						$ooo=substr($content,$start,$end-$start);
						preg_match_all('/<p>/',$ooo,$ma,PREG_OFFSET_CAPTURE);//文字
						$nnn='';
						foreach ($ma[0] as $vs){
							$start=$vs[1];
							$end=stripos($ooo,'</p>',$start+1);
							$nnn.=substr($ooo,$start,$end-$start+4);
						}
						$db_content=$nnn;
						$db_images='';
						break;
					case 'AM730':
						$start=stripos($content,'<div class="article__body" itemprop="articleBody">');
						$end=stripos($content,'<!-- relatedLinks -->',$start);
						$ooo=substr($content,$start,$end-$start);
						$ooo=trim(str_replace('<div class="article__body" itemprop="articleBody">','',$ooo));
						$db_content=$ooo;
						$db_images='';
						break;
					default:
						break;
				}
				
				$data=[
					'users_id'=>1,
					'class_id'=>$class_id,
					'issuer_id'=>$search['id'],
					'title_t'=>$db_title,
					'title_s'=>ZhConvert::zh($db_title,'zh-hans','zh-hant'),
					'content_t'=>$db_content??null,
					'content_s'=>ZhConvert::zh($db_content,'zh-hans','zh-hant')??null,
					'images'=>$db_images,
					'url'=>$new_url,
					'createtime'=>time()
				];
				$words = Sensitive::get(content: $data['title_s']);
				if (empty($words)) {
					\app\model\News::create($data);
				}
			
			}
		
			$this->output->write('成功',true);
		}catch (\Exception $exception){
			$this->output->write($exception->getMessage().$exception->getLine(),true);
			
		}
	}
	
	function curlRequest($url, $header=array(), $params = array(), $is_post = false, $time_out = 10)
	{
		if (!$header){
			$header=array(
				'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0',
				'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
				'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
				'Accept-Encoding: gzip, deflate, br',
				'Connection: keep-alive',
				'Upgrade-Insecure-Requests: 1',
				'Sec-Fetch-Dest: document',
				'Sec-Fetch-Mode: navigate',
				'Sec-Fetch-Site: cross-site'
			);
		}
		
		$str_cookie = isset($ext_params['str_cookie']) ? $ext_params['str_cookie'] : '';
		$ch = curl_init();//初始化curl
		curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		//当需要通过curl_getinfo来获取发出请求的header信息时，该选项需要设置为true
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch,CURLOPT_ENCODING,'gzip');
		curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time_out);
		curl_setopt($ch, CURLOPT_POST, $is_post);
		if ($is_post) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}
		if ($str_cookie) {
			curl_setopt($ch, CURLOPT_COOKIE, $str_cookie);
		}
		if ($header) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		}
		$response = curl_exec($ch);
		//打印请求的header信息
		curl_close($ch);
		return $response;
	}
	

	
	
}